// pages/search/search.js
const service = require('../../../utils/service.js')
const api = require('../../../utils/util.js')
Page({
  data: {
    moneyList: [{
      min_goods_amount: '',
      amount: ''
    }],
    satrtTimeVal: '选择日期时间 ...',
    entTimeVal: '选择日期时间 ...',
    nowSTime: '',
    nowETime: '',
    timeSBol: false,
    timeEBol: false,
    moneyTitle: '',
    isTimeSelect: false,
    saveBol: true
  },

  onLoad: function(options) {
    let oDate = new Date(),
      year = oDate.getFullYear(),
      month = oDate.getMonth() + 1 > 9 ? oDate.getMonth() + 1 : '0' + (oDate.getMonth() + 1),
      day = oDate.getDate() > 9 ? oDate.getDate() : '0' + oDate.getDate();
    this.setData({
      nowSTime: `${year}-${month}-${day}`,
      timeEBol: `${year}-${month}-${day}`
    })
  },
  init: function() {

  },
  //--标题
  inputTitle: function(et) {
    this.setData({
      moneyTitle: et.detail.value || ''
    });
  },
  //--选择开始时间
  startTimeChange: function(et) {
    this.setData({
      satrtTimeVal: et.detail.value,
      timeSBol: true
    });
  },
  //--结束时间
  endTimeTap: function() {
    if (!this.data.timeSBol) {
      api.toast("请选择开始时间！");
      return;
    }
  },
  //--选择结束时间
  endTimeChange: function(et) {
    let s = (new Date(this.data.satrtTimeVal)).getTime();
    let e = (new Date(et.detail.value)).getTime();
    if (s >= e) {
      api.toast("结束时间不能小于等于开始时间!");
      return;
    }
    this.setData({
      entTimeVal: et.detail.value,
      timeEBol: true
    });
  },
  //--添加下一级
  addTemplateast: function() {
    this.data.moneyList.push({
      min_goods_amount: '',
      amount: ''
    });
    this.setData({
      moneyList: this.data.moneyList
    });
  },
  //--添加最大金额
  addMaxMoney: function(et) {
    let index = et.currentTarget.dataset.index;
    let val = et.detail.value.replace(/\s+/g, "");
    let bol = false;
    if (!/^(([1-9][0-9]{0,})|(0{1}))(([0-9]{0,})|(\.[0-9]{0,}))$/.test(val) && val != '') {
      api.toast("请输入正确的金额！");
      bol = true;
    }
    if (bol) {
      val = this.data.moneyList[index].min_goods_amount;
    }
    this.setData({
      [`moneyList[${index}].min_goods_amount`]: val
    });
  },
  //--添加最小金额
  addMinMoney: function(et) {
    let index = et.currentTarget.dataset.index;
    let val = et.detail.value.replace(/\s+/g, "");
    let bol = false;
    if (!/^(([1-9][0-9]{0,})|(0{1}))(([0-9]{0,})|(\.[0-9]{0,}))$/.test(val) && val != '') {
      api.toast("请输入正确的金额！");
      bol = true;
    }
    if (+val >= +this.data.moneyList[index].min_goods_amount && val != '') {
      api.toast('优惠金额不能大于等于消费金额！');
      bol = true;
    }
    if (bol) {
      val = this.data.moneyList[index].amount;
    }
    this.setData({
      [`moneyList[${index}].amount`]: val
    });
  },
  //--删除
  delTemplateast: function(et) {
    if (this.data.moneyList.length <= 1) {
      api.toast("不能在删除了！");
      return;
    }
    let index = +et.currentTarget.dataset.index;
    this.data.moneyList.splice(index, 1);
    this.setData({
      moneyList: this.data.moneyList
    });
  },
  //--保存优惠券
  saveActivity: function() {
    let shop_code = wx.getStorageSync('shopInfo').shop_code,
      self = this,
      shopData = wx.getStorageSync('shop_data_shop');
    if (!this.data.saveBol) {
      return;
    }
    if (this.data.moneyTitle == '') {
      api.toast("请输入标题！");
      return;
    }
    if (!this.data.timeEBol) {
      api.toast("请选择结束时间！");
      return;
    }
    if (!this.data.timeSBol) {
      api.toast("请选择开始时间！");
      return;
    }
    for (let i = 0; i < this.data.moneyList.length; i++) {
      if (this.data.moneyList[i].min_goods_amount == '' || this.data.moneyList[i].amount == '') {
        api.toast("请输入满减金额！");
        return;
      }
    }
    this.setData({
      saveBol: false
    });
    api.ajax(service.url('setCouponActive'), {
      unionid: shopData.shop_unionid,
      token: shopData.token,
      token_key: shopData.token_key,
      shop_code: shop_code,
      active_name: self.data.moneyTitle,
      start_time: self.data.satrtTimeVal,
      end_time: self.data.entTimeVal,
      coupon: JSON.stringify(self.data.moneyList),
    }, function(res) {
      console.log("addCoupon：" + JSON.stringify(res));
      if (res.code == 0) {
        wx.showModal({
          title: "温馨提示",
          cancelText: "返回",
          confirmText: '继续添加',
          content: '添加店铺活动成功！',
          success: function(res) {
            console.log(res);
            self.setData({
              saveBol: true
            });
            if (res.confirm) {
              self.setData({
                moneyList: [{
                  min_goods_amount: '',
                  amount: ''
                }],
                satrtTimeVal: '选择日期时间 ...',
                entTimeVal: '选择日期时间 ...',
                nowSTime: '',
                nowETime: '',
                timeSBol: false,
                timeEBol: false,
                moneyTitle: '',
                isTimeSelect: false
              });
            } else {
              wx.navigateBack();
            }
          },
          fail: function() {
            api.toast("微信API弹窗调用失败！");
          }
        });
      } else {
        api.toast(res.message || '添加失败！');
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})